package com.hunan.api.catv.action.activity;

import com.hunan.api.catv.action.BaseAction;
import com.hunan.api.catv.annotation.MemberAuth;
import com.hunan.api.catv.common.Api;
import com.hunan.api.catv.entity.Result;
import com.hunan.api.catv.entity.vo.NewsVo;
import com.hunan.api.catv.service.biz.activity.ActivityBiz;
import com.hunan.api.catv.service.biz.activity.ActivityVideoBiz;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(Api.activity)
public class ActivityAction extends BaseAction {

    @Autowired
    CacheBiz cacheBiz;

    @Autowired
    ActivityBiz activityBiz;

    @Autowired
    ActivityVideoBiz activityVideoBiz;

    @GetMapping("/list")
    public Result list(int pageNum, HttpServletRequest request) {
        //return buildOK(cacheBiz.activityPageCache(pageNum,getPageSize(request)),"");
        return buildOK(activityBiz.page(pageNum, getPageSize(request)), "");
    }

    @GetMapping("/info")
    public Result info(Integer activityId) {
        if (ObjectUtils.isEmpty(activityId)) {
            return buildRequire("activityId require");
        }
        return buildOK(cacheBiz.activityInfoCache(activityId), "");
    }

    @MemberAuth
    @PostMapping("/participate")
    public Result participate(HttpServletRequest request, Integer flag, Integer activityId) {
        if (ObjectUtils.isEmpty(flag)) {
            return buildRequire("flag require");
        }
        if (ObjectUtils.isEmpty(activityId)) {
            return buildRequire("activityId require");
        }
        return buildResult(activityBiz.participate(flag, getMemberId(request), activityId));
    }

    @GetMapping("/query")
    public Result query(HttpServletRequest request, Integer activityId) {
        return buildOK(activityBiz.query(getMemberToken(request), activityId), "");
    }

    @MemberAuth
    @GetMapping("/myList")
    public Result myList(HttpServletRequest request) {
        return buildOK(activityBiz.list(getMemberId(request)), "");
    }

    @MemberAuth
    @PostMapping("/upload")
    public Result upload(HttpServletRequest request, Integer activityId, String fileId, String cover) {
        if (ObjectUtils.isEmpty(fileId)) {
            return buildRequire("fileId require");
        }
        if (ObjectUtils.isEmpty(activityId)) {
            return buildRequire("activityId require");
        }
        return buildResult(activityVideoBiz.upload(activityId, getMemberId(request), fileId, cover));
    }

    @GetMapping("/video")
    public Result video(HttpServletRequest request, Integer activityId, Integer pageNum, Integer pageSize) {
        return buildOK(activityVideoBiz.page(pageNum, pageSize, activityId), "");
    }

    @MemberAuth
    @GetMapping("/jumpPath")
    public Result jumpPath(HttpServletRequest request, String jumpPath) {
        return buildResult(activityBiz.jumpPath(getMemberId(request), jumpPath));
    }

    @MemberAuth
    @PostMapping("/uploadNews")
    public Result uploadNews(HttpServletRequest request, Integer activityId, NewsVo newsVo) {
        if (ObjectUtils.isEmpty(newsVo.getFileId())) {
            return buildRequire("fileId require");
        }
        if (ObjectUtils.isEmpty(activityId)) {
            return buildRequire("activityId require");
        }
        return buildResult(activityVideoBiz.uploadNews(activityId, getMemberId(request), newsVo));
    }

    @MemberAuth
    @GetMapping("/myNews/{activityId}")
    public Result myNews(HttpServletRequest request, @PathVariable Integer activityId) {
        return buildOK(activityVideoBiz.myNews(activityId, getMemberId(request)), "");
    }

    @GetMapping("/news")
    public Result news(Integer activityId, Integer pageNum, Integer pageSize) {
        return buildOK(activityVideoBiz.news(activityId, pageNum, pageSize), "");
    }

}
